package com.shu.logsearch.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring容器工具类
 * @author Tree Cai
 * @date 2019/7/10
 */
@Component
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext_;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContext_ = applicationContext;
    }

    public static Object getBean(String beanId) {
        return applicationContext_.getBean(beanId);
    }

    public static <T> T getBean(Class<T> tClass) {
        return applicationContext_.getBean(tClass);
    }

    public static void test() {
        for (String s : applicationContext_.getBeanDefinitionNames()) {
            System.out.println(s);
        }
    }

}
